package top.hidoctor.auscultation.widget.tree;

import android.util.Log;

import java.util.ArrayList;
import java.util.List;


public class TreeNode {

    private String type; //0 单选 1 多选
    private String id;
    private String pId;
    private String title;
    private int level;
    private boolean isExpand = true; //默认全部展开
    private boolean checked = false;
    private List<TreeNode> children = new ArrayList<>();
    private TreeNode parent;
    private Object value;
    private RViewHolder mViewHolder;

    public TreeNode(String id, String pId, String title, String type, Object value) {
        super();
        this.id = id;
        this.pId = pId;
        this.title = title;
        this.type = type;
        this.value = value;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPid() {
        return pId;
    }

    public void setPid(String pId) {
        this.pId = pId;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    public boolean isExpand() {
        return isExpand;
    }

    public void setExpand(boolean isExpand) {
        this.isExpand = isExpand;
        if (!isExpand) {
            for (TreeNode node : children) {
                node.setExpand(false);
            }
        }
    }

    public boolean isRoot() {
        return parent == null;
    }

    public boolean isLeaf() {
        return children.size() == 0;
    }

    public List<TreeNode> getChildren() {
        return children;
    }

    public void setChildren(List<TreeNode> children) {
        this.children = children;
    }

    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        this.parent = parent;
    }

    public boolean isParentExpand() {
        return parent != null && parent.isExpand();
    }

    public Object getValue() {
        return value;
    }

    public RViewHolder getViewHolder() {
        return mViewHolder;
    }

    public void setViewHolder(RViewHolder viewHolder) {
        mViewHolder = viewHolder;
    }

    public boolean isChecked() {
        return checked;
    }


    /**
     * @param checked     是否选择
     * @param updateChild 是否有child 以及更新
     */
    public void setChecked(boolean checked, boolean updateChild, boolean multipleChoice) {
        Log.e("TAG", "setChecked: ");
        if (multipleChoice) {//单选
            if (parent != null && parent.children != null && parent.children.size() > 0) {
                for (TreeNode node : parent.children) {
                    node.setChecked(false, true, (node.getParent() != null && node.getParent().getType() == "0") ? true : false);
                }
            }
        }
        this.checked = checked;
        updateParentState();

        if (updateChild) {
            updateChildState(checked);
        }

    }


    private void updateChildState(boolean checked) {

        for (int i = 0; i < children.size(); i++) {
            if (checked) {
                children.get(0).setChecked(true, true, (children.get(0).getParent() != null && children.get(0).getParent().getType() == "0") ? true : false);
            } else {
                children.get(i).setChecked(false, true, (children.get(i).getParent() != null && children.get(i).getParent().getType() == "0") ? true : false);
            }
        }
    }

    /**
     * 设置父节点
     */
    private void updateParentState() {
        if (parent != null) {
            boolean allCheck = true;
            for (TreeNode node : parent.children) {
                if (node.checked) {
                    allCheck = true;
                    break;
                } else {
                    allCheck = false;
                }
            }
            if (allCheck) {
                parent.setChecked(true, false, false);
            } else {
                parent.setChecked(false, false, false);
            }
        }
    }
}
